package com.anlen.call.util;

import java.util.HashMap;
import java.util.List;

import com.anlen.call.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {
	private int[] colors = new int[] { 0xff3cb371, 0xffa0a0a0 };
	private Context mContext;
	private List<HashMap<String, Object>> dataList;

	public MyBaseAdapter(Context context, List<HashMap<String, Object>> dataList) {
		this.mContext = context;
		this.dataList = dataList;
	}
	public int getCount() {
		return dataList.size();
	}

	@Override
	public HashMap<String, Object> getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.goods_view, null);
			holder.image = (ImageView) convertView.findViewById(R.id.imag);
			holder.title = (TextView) convertView.findViewById(R.id.tite);
			holder.text = (TextView) convertView.findViewById(R.id.text);

			// 将holder绑定到convertView
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 向ViewHolder中填入的数据
		holder.image.setImageResource((Integer) getItem(position).get("imag"));
		holder.title.setText((String) getItem(position).get("title"));
		holder.text.setText((String) getItem(position).get("text"));

		int colorPos = position % colors.length;
		convertView.setBackgroundColor(colors[colorPos]);

		return convertView;
		}
	/**
	 * ViewHolder类用以储存item中控件的引用
	 */
	final class ViewHolder {
		ImageView image;
		TextView title;
		TextView text;
	}
}
